package com.pxxy.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;


import com.pxxy.mall.product.dto.attrgroup.AttrgroupReqDTO;
import com.pxxy.mall.product.service.AttrAttrgroupRelationService;
import com.pxxy.mall.product.service.AttrService;
import com.pxxy.mall.product.service.CategoryService;
import com.pxxy.mall.product.vo.AttrAttrgroupRelationVo;
import com.pxxy.mall.product.vo.AttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pxxy.mall.product.entity.AttrGroupEntity;
import com.pxxy.mall.product.service.AttrGroupService;
import com.pxxy.common.utils.PageUtils;
import com.pxxy.common.utils.R;


/**
 * 属性分组
 *
 * @author Zhangjj
 * @email zhangjianjian@gmail.com
 * @date 2023-11-29 17:05:08
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;


    @Autowired
    private CategoryService categoryService;


    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;


    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithattr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupVo> list = attrGroupService.getAttrGroupWithattr(catelogId);
        return R.ok().put("data", list);
    }


    /**
     * 列表
     */
    @GetMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 查询分组与属性的关联
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrAttrgroupRelationVo> list = attrGroupService.queryAttrRelation(attrGroupId);
        return R.ok().put("data", list);
    }

    /**
     * 获取属性分组没有关联的其他属性
     *
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params, @PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page = attrService.queryNoattrRelationPage(params, attrGroupId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        if (Objects.nonNull(attrGroup)) {
            Long catelogId = attrGroup.getCatelogId();
            Long[] path = categoryService.findCatelogPath(catelogId);
            attrGroup.setCatelogPath(path);
        }

        return R.ok().put("attrGroup", attrGroup);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrgroupReqDTO> reqDTOS) {

        relationService.saveAttrRelation(reqDTOS);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody List<AttrgroupReqDTO> attrgroupReqDTO) {
        attrGroupService.removeMoreByIds(attrgroupReqDTO);

        return R.ok();
    }


}
